package com.ptc.ims.server;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.ptc.ims.client.InventoryService;
import com.ptc.ims.shared.InventoryException;
import com.ptc.ims.shared.Product;

public class InventoryServiceImpl 
extends RemoteServiceServlet
implements InventoryService
{
	private static final long serialVersionUID = 2377276698897608858L;
	private static final Logger LOGGER = LoggerFactory.getLogger(InventoryServiceImpl.class);

	@Override
	public List<Product> fetchProducts()	
	{
		LOGGER.info("Start fetchProducts");
		List<Product> products = null;
		
		try 
		{
			products = InventoryDAO.getInstance().getItems();
		} 
		catch (InventoryException e) 
		{
			products = new ArrayList<Product>();
			LOGGER.error("An exception has occurred", e);
		}
		
		LOGGER.info("End fetchProducts, returning {} products", products.size());
		return products;
	}

	@Override
	public boolean modifyProduct(Product newValues)	
	{
		LOGGER.info("Start modifyProduct, id={}", newValues.getId());
		boolean modifiedInventory = true;
		
		try 
		{
			InventoryDAO.getInstance().editItem(newValues.getId(), newValues);
		} 
		catch (InventoryException e) 
		{
			modifiedInventory = false;
			LOGGER.error("An exception has occurred", e);
		}
		
		LOGGER.info("End modifyProduct, modify successful={}", modifiedInventory);
		return modifiedInventory;
	}

	@Override
	public String addProduct(Product newItem)
	{
		LOGGER.info("Start addProduct");		
		String productId = InventoryDAO.getInstance().generateID();
		
		try
		{
			newItem.setId(productId);
			InventoryDAO.getInstance().addItem(productId, newItem);
		}
		catch (InventoryException e) 
		{
			productId = null;
			LOGGER.error("An exception has occurred", e);
		}
		LOGGER.info("End addProduct, add successful={}", productId != null);
		return productId;
	}

	@Override
	public Product fetchProduct(String productId) 
	{
		LOGGER.info("Start fetchProduct, id={}", productId);
		
		Product result = null;
				
		try 
		{
			for (Product product : InventoryDAO.getInstance().getItems())
			{
				if (product.getId().equals(productId))
				{
					result = product;
					break;
				}
			}
		} 
		catch (InventoryException e) 
		{
			LOGGER.error("An exception has occurred", e);			
		}
		
		LOGGER.info("Start fetchProduct, found product={}", result != null);
		return result;
	}

	@Override
	public List<String> removeProduct(List<String> productIds) 
	{
		LOGGER.info("Start removeProduct");
		List<String> removedProducts = new ArrayList<String>();		
		
		try
		{
			for (String productId : productIds)
			{
				removedProducts.add(productId);
				InventoryDAO.getInstance().deleteItem(productId);
			}			
		} 
		catch (InventoryException e) 
		{			
			LOGGER.error("An exception has occurred", e);
		}
		
		LOGGER.info("End removed products size={}", removedProducts.size());
		return removedProducts;
	}

}
